import React, { FC } from 'react';
import { Button, Modal, Space } from 'antd';
import styles from './index.less';
import IconFont from '@/components/IconFont';
import { useModel } from 'umi';
import { modelName } from '../../index';
import useCardModel from '@/pages/Card/useCardModel';
import useHoxModel from '../../useHoxModel';
import { ActionType } from '@/components/ProTable';
import ConfirmProps from '@/components/CommonModal/Confirm';
const { confirm } = Modal;

interface ToolBarProps {
  actionRef: React.MutableRefObject<ActionType | undefined>;
}

const ToolBar: FC<ToolBarProps> = ({ actionRef }) => {
  const { showModal } = useModel('useCommonModal', (model) => ({ showModal: model.showModal }));
  const { getCardType, deleteCard } = useCardModel((model) => [model.getCardType]);
  const { selectedRowKeys, dispatch } = useHoxModel((model) => [model.selectedRowKeys]);
  return (
    <div className={styles['toolbar-wrap']}>
      <div className={styles['left']}>
        <Space>
          <Button
            className={styles['add']}
            type="primary"
            onClick={() => {
              getCardType();
              showModal(modelName + 'add');
            }}
          >
            <IconFont type={'iconxinzeng1'} />
            <span>新增</span>
          </Button>
          <Button
            type="primary"
            onClick={() => {
              confirm({
                ...new ConfirmProps('是否删除选中的所有记录?'),
                async onOk() {
                  const res = await deleteCard({ ids: selectedRowKeys });
                  if (res?.result === 0) {
                    dispatch({ selectedRowKeys: [] });
                    actionRef.current?.reloadAndRest();
                  }
                },
              });
            }}
            disabled={!selectedRowKeys[0]}
            className={styles['delete']}
          >
            <IconFont type={'iconshanchu1'} />
          </Button>
        </Space>
      </div>
      <div className={styles['right']}></div>
    </div>
  );
};
export default ToolBar;
